package com.luhuiguo.sso.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.luhuiguo.sso.Constants;
import com.luhuiguo.sso.dao.RoleDao;
import com.luhuiguo.sso.entity.Role;
import com.luhuiguo.sso.service.RoleManager;

public class RoleManagerImpl implements RoleManager {

	private RoleDao roleDao;

	@Required
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	@Override
	@Transactional(readOnly = true)
	public Role getRole(String name) {
		List<Role> roles = roleDao.findByProperty("name", name); //$NON-NLS-1$
		if (roles.size() == 0) {
			return null;
		}
		return roles.get(0);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Role> getAllRole(int... rowStartIdxAndCount) {
		return roleDao.findAll(rowStartIdxAndCount);
	}

	@Override
	public boolean existsRole(Long id) {
		return roleDao.exists(id);
	}

	@Override
	public boolean existsRole(String name) {
		return getRole(name) != null;
	}

	@Override
	public void createRole(Role role) {
		roleDao.create(role);
	}

	@Override
	public void updateRole(Role role) {
		roleDao.update(role);
	}

	@Override
	public void deleteRole(Long id) {
		roleDao.delete(id);
	}

	@Override
	public void deleteRole(Role role) {
		roleDao.delete(role);
	}

	@Override
	public void deleteRole(String name) {
		deleteRole(getRole(name));
	}

	@Override
	public Role getAdminRole() {
		return getRole(Constants.ROLE_ADMIN);

	}

	@Override
	public Role getUserRole() {
		return getRole(Constants.ROLE_USER);
	}

}
